package com.blog.file.controller;



import com.blog.file.service.UserService;
import com.blog.file.utils.OssUtil;
import com.blog.utils.ResponseResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;


import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/oss")
@CrossOrigin("*")
public class OssController {
    @Autowired
    OssUtil ossUtil;  //注入OssUtil
    @Autowired
    UserService userService;
    @PostMapping("/uploadFile/{userId}")
    public ResponseResult fileUpload(@RequestParam("file") MultipartFile file, @PathVariable String userId)
    {
        try {
            String url = ossUtil.uploadFile(file); //调用OSS工具类
           if(!userService.setAvatarUrl(userId, url))
            return ResponseResult.error().message("头像地址保存失败");
            Map<String, Object> returnBody = new HashMap<>();
            returnBody.put("url", url);
            return ResponseResult.ok().message("上传成功").data(returnBody);
        }
        catch (Exception e) {
            return ResponseResult.error().message("上传失败");
        }
    }
}

